package com.woniu108.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu108.carService.commos.dto.ServiceInfoDto;
import com.woniu108.carService.commos.dto.StoreProductDto;
import com.woniu108.carService.commos.exception.WoniuException;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.order.client.PersonClient;
import com.woniu108.order.client.ProductClient;
import com.woniu108.order.mapper.OrderServiceInfoMapper;
import com.woniu108.order.model.dto.OrderServiceInfoDto;
import com.woniu108.order.model.model.OrderServiceInfo;
import com.woniu108.order.service.OrderServiceInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单服务详情表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class OrderServiceInfoServiceImpl extends ServiceImpl<OrderServiceInfoMapper, OrderServiceInfo> implements OrderServiceInfoService {
    @Resource
    private OrderServiceInfoMapper orderServiceInfoMapper;
    @Resource
    private ProductClient productClient;
    @Override
    public List<OrderServiceInfoDto> selectOrderServiceInfoByOrderServiceNum(String orderServiceNum) {
        QueryWrapper<OrderServiceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_service_num",orderServiceNum);
        List<OrderServiceInfo> orderServiceInfos = orderServiceInfoMapper.selectList(queryWrapper);

        ArrayList<OrderServiceInfoDto> orderServiceInfoDtos = new ArrayList<>();
        for (OrderServiceInfo orderServiceInfo:orderServiceInfos){
            OrderServiceInfoDto orderServiceInfoDto = BeanUtil.toBean(orderServiceInfo, OrderServiceInfoDto.class);

            Result<ServiceInfoDto> serviceInfoDtoResult = productClient.findServiceInfoById(orderServiceInfoDto.getServiceId());
            if(serviceInfoDtoResult.getCode()!=200){
                throw new WoniuException(serviceInfoDtoResult.getMsg(),serviceInfoDtoResult.getCode());
            }
            ServiceInfoDto serviceInfoDto = serviceInfoDtoResult.getData();
            orderServiceInfoDto.setServiceInfoDto(serviceInfoDto);

            /**
             * 查询门店商品
             */
            if(serviceInfoDto.getSTName().contains("洗车")){
                //洗车没有商品可查
                orderServiceInfoDto.setStoreProductDto(null);
            }else {
                Result<StoreProductDto> oneStoreProductResult = productClient.findOneStoreProductById(orderServiceInfo.getSpInfoId());
                if(oneStoreProductResult.getCode()!=200){
                    throw new WoniuException(oneStoreProductResult.getMsg(),oneStoreProductResult.getCode());
                }
                StoreProductDto storeProductDto = oneStoreProductResult.getData();
                orderServiceInfoDto.setStoreProductDto(storeProductDto);
            }
            //加入集合
            orderServiceInfoDtos.add(orderServiceInfoDto);
        }
        //返回集合
        return orderServiceInfoDtos;
    }
}
